package com.yzg.controller;

import com.yzg.pojo.Permission;
import com.yzg.service.PermissionService;
import com.yzg.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: decade
 * @Description: 权限菜单管理
 * @date: 2021/1/15 0:02
 */
@Api(tags = "PermissionController", description = "权限菜单管理")
@RestController
@RequestMapping("/acl/permission")
@CrossOrigin
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色Id",required = true,paramType = "String"),
            @ApiImplicitParam(name = "permissionIds",value = "菜单Id数组",required = true,paramType = "String[]"),
    })
    public R doAssign(String roleId, String[] permissionIds) {
        permissionService.saveRolePermissionRealtion(roleId,permissionIds);
        return R.ok();
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public R toAssign(
            @ApiParam(name = "roleId" ,value = "角色id", required = true)
            @PathVariable String roleId
         ){

        List<Permission> list = permissionService.selectAllMenu(roleId);
        return R.ok().data("children", list);
    }

    @ApiOperation(value = "获取所有菜单")
    @GetMapping("/getList")
    public R getAllPermission(){
        List<Permission> list =  permissionService.queryAllMenu();
        return R.ok().data("children",list);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "permission" ,value = "权限信息", required = true)
            @RequestBody Permission permission
    ){
        permissionService.save(permission);
        return R.ok();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "permission" ,value = "菜单对象", required = true)
            @RequestBody Permission permission
    ){
        permissionService.updateById(permission);
        return R.ok();
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("/remove/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "菜单Id",required = true,paramType = "String"),
    })
    public R remove(@PathVariable String id) {
        permissionService.removeChildById(id);
        return R.ok();
    }
}
